package com.tim.app.server.api;

import android.content.Context;
import android.text.TextUtils;

import com.application.library.net.ResponseCallback;
import com.application.library.util.NetUtils;
import com.lzy.okhttputils.cache.CacheMode;
import com.tim.app.RT;
import com.tim.app.server.logic.UserManager;
import com.tim.app.server.net.HttpMethod;
import com.tim.app.server.net.NetworkInterface;
import com.tim.app.util.PhoneInfoUtil;

import java.util.HashMap;

/**
 * 统计
 */
public class API_Statistics {

    private static volatile API_Statistics api = null;

    private static String API_STATICSTICS_SCHEME = "";

    public static final String LOOK_AT_DETAIl = "/moli_quartz/v1/acquisition/addDataLogs";//查看详情


    private API_Statistics() {

    }

    public static API_Statistics ins() {
        if (api == null) {
            synchronized (API_Statistics.class) {
                if (api == null) {
                    api = new API_Statistics();
                    switch (RT.HOST) {
                        case DEVELOP:
                            API_STATICSTICS_SCHEME = "http://192.168.1.26:8082";
                            break;
                        case DEBUG:
                            API_STATICSTICS_SCHEME = "";
                            break;
                        case PUBLISH:
                            API_STATICSTICS_SCHEME = "";
                            break;
                    }
                }
            }
        }
        return api;
    }


    /**
     * 统计详情
     * http://doc.java.duailin.top/api/project.do#/ffff-1496814745348-1722617555-0002/front/interfaceDetail/ffff-1496816840200-1722617555-0010
     *
     * @param tag
     */
    public void addDetailData(String tag, Context context, String type, String typeId) {
        String url = API_STATICSTICS_SCHEME + LOOK_AT_DETAIl;
        HashMap params = new HashMap();
        if (UserManager.ins().isLogin()) {
            params.put("user_id", UserManager.ins().getUserId());
        }
        params.put("data_type", "look");
        params.put("type_id", typeId);
        params.put("type", type);
        params.putAll(getStatisticsData(context));

        NetworkInterface.ins().connected(HttpMethod.POST, url, tag, params, CacheMode.DEFAULT, false, new ResponseCallback() {
            @Override
            public boolean onResponse(Object resultValue, int status, String errmsg, int id, boolean fromcache) {
                return false;
            }
        });
    }

    /**
     * 统计注册消息
     * http://doc.java.duailin.top/api/project.do#/ffff-1496814745348-1722617555-0002/front/interfaceDetail/ffff-1496816823263-1722617555-0009
     *
     * @param tag
     * @param context
     * @param type
     */
    public void addRegistData(String tag, Context context, String type) {
        String url = API_STATICSTICS_SCHEME + LOOK_AT_DETAIl;
        HashMap params = new HashMap();
        params.putAll(getStatisticsData(context));
        params.put("data_type", "register");
        params.put("type", type);
        if (UserManager.ins().isLogin()) {
            params.put("user_id", UserManager.ins().getUserId());
        }
        NetworkInterface.ins().connected(HttpMethod.POST, url, tag, params, CacheMode.DEFAULT, false, new ResponseCallback() {
            @Override
            public boolean onResponse(Object resultValue, int status, String errmsg, int id, boolean fromcache) {
                return false;
            }
        });
    }

    /**
     * 统计激活信息
     *
     * @param tag
     * @param context
     */
    public void addctivationData(String tag, Context context) {
        String url = API_STATICSTICS_SCHEME + LOOK_AT_DETAIl;
        HashMap params = new HashMap();
        params.putAll(getStatisticsData(context));
        params.put("data_type", "activation");
        params.put("user_id", "0");
        NetworkInterface.ins().connected(HttpMethod.POST, url, tag, params, CacheMode.DEFAULT, false, new ResponseCallback() {
            @Override
            public boolean onResponse(Object resultValue, int status, String errmsg, int id, boolean fromcache) {
                return false;
            }
        });
    }

    /**
     * 统计登录数据
     * http://doc.java.duailin.top/api/project.do#/ffff-1496814745348-1722617555-0002/front/interfaceDetail/ffff-1496816795264-1722617555-0007
     *
     * @param tag
     * @param context
     * @param type
     */
    public void addLoginData(String tag, Context context, String type, String type_info) {
        String url = API_STATICSTICS_SCHEME + LOOK_AT_DETAIl;
        HashMap params = new HashMap();
        params.putAll(getStatisticsData(context));
        params.put("data_type", "login");
        if (UserManager.ins().isLogin()) {
            params.put("user_id", UserManager.ins().getUserId());
        } else {
            params.put("user_id", "0");
        }
        params.put("type", type);
        params.put("type_info", type_info);
        NetworkInterface.ins().connected(HttpMethod.POST, url, tag, params, CacheMode.DEFAULT, false, new ResponseCallback() {
            @Override
            public boolean onResponse(Object resultValue, int status, String errmsg, int id, boolean fromcache) {
                return false;
            }
        });
    }

    /**
     * 统计分享数据
     *
     * @param tag
     * @param context
     * @param type
     */
    public void addShareData(String tag, Context context, String type) {
        String url = API_STATICSTICS_SCHEME + LOOK_AT_DETAIl;
        HashMap params = new HashMap();
        params.putAll(getStatisticsData(context));
        params.put("data_type", "share");
        if (UserManager.ins().isLogin()) {
            params.put("user_id", UserManager.ins().getUserId());
        }
        params.put("type", type);
        NetworkInterface.ins().connected(HttpMethod.POST, url, tag, params, CacheMode.DEFAULT, false, new ResponseCallback() {
            @Override
            public boolean onResponse(Object resultValue, int status, String errmsg, int id, boolean fromcache) {
                return false;
            }
        });
    }

    /**
     * @param tag
     * @param context
     * @param type
     */
    public void addActionData(String tag, Context context, String type) {
        String url = API_STATICSTICS_SCHEME + LOOK_AT_DETAIl;
        HashMap params = new HashMap();
        params.putAll(getStatisticsData(context));
        params.put("data_type", "action");
        if (UserManager.ins().isLogin()) {
            params.put("user_id", UserManager.ins().getUserId());
        }
        params.put("type", type);
        NetworkInterface.ins().connected(HttpMethod.POST, url, tag, params, CacheMode.DEFAULT, false, new ResponseCallback() {
            @Override
            public boolean onResponse(Object resultValue, int status, String errmsg, int id, boolean fromcache) {
                return false;
            }
        });
    }

    /**
     * 获取统计数据
     * <p>
     * * @param context
     *
     * @return
     */
    public HashMap getStatisticsData(Context context) {
        HashMap params = new HashMap();
        if (UserManager.ins().isLogin() && null != UserManager.ins().getLoginUser() && !TextUtils.isEmpty(UserManager.ins().getLoginUser().getPlatformMark())) {
            params.put("platform_id", UserManager.ins().getLoginUser().getPlatformMark());
        } else {
            params.put("platform_id", RT.AppInfo.Channel);
        }
        String location = PhoneInfoUtil.getLocation(context);
        if (!TextUtils.isEmpty(location) && location.contains("|")) {
            params.put("longitude", location.split("\\|")[1]);
            params.put("latitude", location.split("\\|")[0]);
        }
        params.put("network_type", NetUtils.getNetWorkType(context));
        params.put("phone_type", PhoneInfoUtil.getDeviceModel());
        params.put("phone_id", PhoneInfoUtil.getUuid(context));

//        params.put("uuid", PhoneInfoUtil.getUuid(context));

        params.put("edition", PhoneInfoUtil.getAppVersionName(context));
        params.put("system", "Android");
        params.put("client_package_name", "com.moli.jasmine");
        return params;
    }
}
